package cfm.com.cfmscreen.comm;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import org.java_websocket.client.WebSocketClient;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Allen Lin on 2016/02/17.
 */
public class MyApp extends Application {

    private static Context sContext;
    public static RequestQueue requestQueue;
    public static WebSocketClient mWebSocketClient;

    @Override
    public void onCreate() {
        super.onCreate();
        sContext = getApplicationContext();
        requestQueue = Volley.newRequestQueue(sContext);

//        tts = new TextToSpeech(sContext, null);
//        tts.setPitch(1.0f);
//        tts.setSpeechRate(0.98f);

        int pid = android.os.Process.myPid();
        String processAppName = getAppName(pid);
        if (processAppName == null ||!processAppName.equalsIgnoreCase(sContext.getPackageName())) {
            Log.e("ww", "enter the service process!");

            // 则此application::onCreate 是被service 调用的，直接返回
            return;
        }

    }

    public static Context getContext() {
        return sContext;
    }


    private String getAppName(int pID) {
        String processName = null;
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        PackageManager pm = this.getPackageManager();
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
            try {
                if (info.pid == pID) {
                    processName = info.processName;
                    return processName;
                }
            } catch (Exception e) {
                // Log.d("Process", "Error>> :"+ e.toString());
            }
        }
        return processName;
    }


}

